<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="用户id" prop="customerid">
      <el-input v-model="dataForm.customerid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="食品id" prop="foodid">
      <el-input v-model="dataForm.foodid" placeholder="食品id"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="price">
      <el-input v-model="dataForm.price" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="num">
      <el-input v-model="dataForm.num" placeholder="数量"></el-input>
    </el-form-item> -->
    <el-form-item label="是否结算" prop="ischarge">
     <!--  <el-input v-model="dataForm.ischarge" placeholder="是否结算(0:未结算；1:已结算)"></el-input> -->
     <el-radio-group v-model="dataForm.ischarge">
          <el-radio :label="0">未结算</el-radio>
          <el-radio :label="1">已结算</el-radio>
        </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          customerid: '',
          foodid: '',
          price: '',
          num: '',
          ischarge: 0
        },
        dataRule: {
          customerid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          foodid: [
            { required: true, message: '食品id不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          ischarge: [
            { required: true, message: '是否结算(0:未结算；1:已结算)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cart/cart/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.customerid = data.cart.customerid
                this.dataForm.foodid = data.cart.foodid
                this.dataForm.price = data.cart.price
                this.dataForm.num = data.cart.num
                this.dataForm.ischarge = data.cart.ischarge
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cart/cart/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'customerid': this.dataForm.customerid,
                'foodid': this.dataForm.foodid,
                'price': this.dataForm.price,
                'num': this.dataForm.num,
                'ischarge': this.dataForm.ischarge
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
